package com.loma.common.utils.ext


import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by xiaojianjun on 2019-11-21.
 */
/**
 * 沉浸式系统UI，将系统UI(状态栏和导航栏)设为透明，并且主布局沉浸到系统UI下面。
 * @param [light] true-状态栏字体和图标为黑色，false-状态栏图标为白色
 */
fun Date.yyyyMMdd(): String {
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
    return simpleDateFormat.format(this)
}

fun Date.MMdd(): String {
    val simpleDateFormat = SimpleDateFormat("MM-dd")
    return simpleDateFormat.format(this)
}

fun Date.yyyyMMdd_HHmm(): String {
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
    return simpleDateFormat.format(this)
}

fun Date.HHmm(): String {
    val simpleDateFormat = SimpleDateFormat("HH:mm")
    return simpleDateFormat.format(this)
}

fun Date.yyyyMMdd_HHmmss(): String {
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    return simpleDateFormat.format(this)
}

fun String.yyyyMMdd(): Date {
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
    return simpleDateFormat.parse(this)
}
fun String.yyyy_MM_dd(): String {
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
    return simpleDateFormat.parse(this).yyyyMMdd()
}

fun String.yyyyMMdd_HHmmss(): Date {
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    return simpleDateFormat.parse(this)
}

/**
 * @param days 天数
 * @return 向后计算多少天
 */
fun Date.addDays(days: Int): Date {
    val cal = Calendar.getInstance()
    var simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
    var n = simpleDateFormat.format(this)
    var yymmdd = simpleDateFormat.parse(n)

    cal.time = yymmdd
    cal.add(Calendar.DATE, days) //minus number would decrement the days

    return cal.time
}

/**
 * @param field Calendar字段
 * @param v 向后计算的值
 * @sample Calendar.YEAR
 *@sample Calendar.MONTH,
 * @return 从当前时间的@{field}向后推算 + v
 */
fun Date.add(field: Int, v: Int): Date {
    val cal = Calendar.getInstance()
    cal.time = this
    cal.add(field, v) //minus number would decrement the days
    cal.time.log()
    return cal.time
}

/**

 * @return 下周1 00:00:00的date
 */
fun Date.getNextMonday(): Date {
    val cal = Calendar.getInstance()
    cal.time = this
    val week = cal[Calendar.DAY_OF_WEEK]
    if (week > 2) {
        cal.add(Calendar.DAY_OF_MONTH, -(week - 2) + 7)
    } else {
        cal.add(Calendar.DAY_OF_MONTH, 2 - week + 7)
    }
    cal.set(Calendar.HOUR, 0)
    cal.set(Calendar.MINUTE, 0)
    cal.set(Calendar.SECOND, 0)
    return cal.time
}


/**

 * @return 从当前时间的日期
 */
fun String.getDateMMdd(): String {
    val date = this.yyyyMMdd_HHmmss()

    val now = Date()
    val oldTime = date.time
    val nowTime = now.time

    val day = (nowTime - oldTime) / (24 * 60 * 60 * 1000)

    return if (day < 1) {  //今天
        val format = SimpleDateFormat("HH:mm")
        format.format(date)
    } else if (day == 1L) {     //昨天
        val format = SimpleDateFormat("HH:mm")
        "昨天 " + format.format(date)
    } else {    //可依次类推
        val format = SimpleDateFormat("MM/dd")
        format.format(date)
    }
}


/**
 * @return 从当前时间的时分
 */
fun String.getDateHHMM(): String {
    val date = this.yyyyMMdd_HHmmss()
    return date.HHmm()


}



